/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.AccountDAO;
import dao.CategoryDAO;
import dao.ManufacturerDAO;
import dao.ProductDAO;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.hibernate.HibernateException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import pojo.Account;
import pojo.Category;
import pojo.Manufacturer;
import pojo.Product;
import ultilities.FS_Ulti;

/**
 *
 * @author Nero
 */
@Controller
public class Home {

    @RequestMapping("/*")
    public ModelAndView Unknow(HttpServletRequest request) {
        return new ModelAndView("pagenotfound");
    }

    @RequestMapping("/home")
    public ModelAndView AdminHome(HttpServletRequest request) {
        return new ModelAndView(new RedirectView("/trangchu.htm?view=danhsachsanpham&page=1&item=21"));
    }

    @RequestMapping("/trangchu")
    //public ModelAndView AdminRedirect(@RequestParam String view, HttpServletRequest request) {
    public ModelAndView AdminRedirect(@RequestParam Map<String, String> params, HttpServletRequest request) {
        String view = (String) params.get("view");
        HttpSession session = request.getSession();
        ModelAndView mav = new ModelAndView("pagenotfound");
        if (view == null) {
            mav = new ModelAndView("home/HomeMasterPage");
            mav.addObject("CONTENT_PAGE", "/WEB-INF/jsp/home/index.jsp");
            List<Product> productList;
            productList = ProductDAO.getTop9NewProduct();
            mav.addObject("NEW_PRODUCT_LIST", productList);
            productList = ProductDAO.getTop9BestsellProduct();
            mav.addObject("BESTSELL_PRODUCT_LIST", productList);

            List<Category> categoryList = CategoryDAO.getCategoryList();
            mav.addObject("CATEGORY_LIST", categoryList);
            List<Manufacturer> manufacturerList = ManufacturerDAO.getManufacturerList();
            mav.addObject("MANUFACTURER_LIST", manufacturerList);

        } else if (view.equals("danhsachsanpham")) {
            int cat = -1;
            if (params.get("cat") != null) {
                cat = Integer.parseInt(params.get("cat").toString());
            }
            if (params.get("page") == null || params.get("item") == null) {
                if (cat == -1) {
                    mav = new ModelAndView(new RedirectView("/trangchu.htm?view=danhsachsanpham&page=1&item=21"));
                } else {
                    mav = new ModelAndView(new RedirectView("/trangchu.htm?view=danhsachsanpham&cat=" + cat + "&page=1&item=20"));
                }
            } else {
                int page = Integer.parseInt(params.get("page").toString());
                int item = Integer.parseInt(params.get("item").toString());
                List<Product> productList;
                productList = ProductDAO.getProductListForView(cat);
                mav = new ModelAndView("home/HomeMasterPage");
                mav.addObject("CONTENT_PAGE", "/WEB-INF/jsp/home/DanhSachSanPham.jsp");
                int nPage = FS_Ulti.PaginationTotalPage(productList, item);
                try {
                    productList = (List<Product>) FS_Ulti.Pagination(productList, page, item);
                    mav.addObject("MESSAGE", "SUCCESS");
                    mav.addObject("CURRENT_PAGE", page);
                    mav.addObject("TOTAL_PAGE", nPage);
                    mav.addObject("ITEM_PER_PAGE", item);
                    mav.addObject("PRODUCT_LIST", productList);
                    mav.addObject("CATEGORY", cat);
                    List<Category> categoryList = CategoryDAO.getCategoryList();
                    mav.addObject("CATEGORY_LIST", categoryList);
                    List<Manufacturer> manufacturerList = ManufacturerDAO.getManufacturerList();
                    mav.addObject("MANUFACTURER_LIST", manufacturerList);
                    ////lấy quảng cáo
                    ////Sản phẩm nổi bật
                } catch (Exception ex) {
                    mav.addObject("MESSAGE", ex.getMessage());
                    mav.addObject("ITEM_PER_PAGE", item);
                    mav.addObject("CATEGORY", cat);
                }
            }
        } else if (view.equals("sanpham")) {
            if (params.get("id") == null) {
                mav = new ModelAndView(new RedirectView("/unknow.htm"));
            } else {
                int id = Integer.parseInt(params.get("id").toString());
                try {
                    Product product = ProductDAO.getProduct(id);
                    mav = new ModelAndView("home/HomeMasterPage");
                    mav.addObject("CONTENT_PAGE", "/WEB-INF/jsp/home/ChiTietSanPham.jsp");
                    mav.addObject("MESSAGE", "SUCCESS");
                    mav.addObject("PRODUCT", product);
                    List<Product> productList = ProductDAO.getSame5CategoryProduct(product.getCategory().getId());
                    mav.addObject("PRODUCT_LIST", productList);
                    List<Category> categoryList = CategoryDAO.getCategoryList();
                    mav.addObject("CATEGORY_LIST", categoryList);
                    List<Manufacturer> manufacturerList = ManufacturerDAO.getManufacturerList();
                    mav.addObject("MANUFACTURER_LIST", manufacturerList);
                    mav.addObject("CATEGORY_DAO", new CategoryDAO());
                    mav.addObject("MANUFACTURER_DAO", new ManufacturerDAO());
                    ////lấy quảng cáo
                    ////Sản phẩm nổi bật
                } catch (Exception ex) {
                    mav.addObject("MESSAGE", ex.getMessage());
                }
            }
        }
        return mav;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public @ResponseBody
    String Login(@ModelAttribute(value = "mydata") LoginInfo mydata, BindingResult result, HttpServletRequest request) {
        String returnValue = "";
        HttpSession session = request.getSession();
        Account currentUser = (Account) session.getAttribute("CURRENT_USER");
        ModelAndView view = new ModelAndView("pagenotfound");
        if (currentUser != null && currentUser.isAccountType()) {
            returnValue = "SUCCESS";
        } else {
            try {
                AccountDAO accountDAO = new AccountDAO();
                Account account = accountDAO.logIn(mydata.getUsername(), mydata.getPassword(), true);
                if (account != null) {
                    session.setAttribute("CURRENT_USER", account);
                    returnValue = "SUCCESS";


                }
            } catch (HibernateException ex) {
                Logger.getLogger(Admin.class
                        .getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                returnValue = ex.getMessage();
            }
        }
        return returnValue;
    }
}
